package com.ptszyxx.popose.module.main.game;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.ptszyxx.popose.BR;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.databinding.FragmentGameBinding;
import com.ptszyxx.popose.module.main.game.adapter.GameAdapter;
import com.ptszyxx.popose.module.main.game.vm.GameVM;
import com.ysg.base.BaseFragment;
import com.ysg.http.data.AppViewModelFactory;
import com.ysg.http.data.entity.game.GameEntity;

import java.util.List;

public class GameFragment extends BaseFragment<FragmentGameBinding, GameVM> {
    private GameAdapter adapter;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_game;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public GameVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(GameVM.class);
    }

    @Override
    public void initAdapter() {
        super.initAdapter();
        adapter = new GameAdapter(viewModel);
        binding.refreshView.initAdapterNoLine(adapter);
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.requestList(true);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.onRefreshEvent.observe(this, new Observer<List<GameEntity>>() {
            @Override
            public void onChanged(List<GameEntity> list) {
                setAdapterUI(list);
            }
        });
    }

    private void setAdapterUI(List<GameEntity> list) {
        binding.refreshView.setAdapterUI(adapter, list, viewModel.isRefresh());
    }

}
